package com.xbongbong.workorder.service;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.CommonHelp;
import com.xbongbong.paas.lowcode.LowCodeConfigService;
import com.xbongbong.paas.pojo.dto.FormDataAddGetDTO;
import com.xbongbong.paas.pojo.vo.FormDataAddGetVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.parent.abstracts.AbstractNewPageRenderService;
import com.xbongbong.parent.explain.FormDataAddFormExplainHandlerName;
import com.xbongbong.pro.enums.PackageTypeEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.teammembers.enums.FormOwnerUniqueEnum;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.pro.lowcodeconfig.pojo.dto.LowCodeConfigGetDTO;
import com.xbongbong.pro.lowcodeconfig.pojo.vo.LowCodeConfigGetVO;
import com.xbongbong.saas.domain.entity.WorkOrderExplainEntity;
import com.xbongbong.saas.domain.entity.WorkOrderFormEntity;
import com.xbongbong.saas.domain.entity.WorkOrderTemplateEntity;
import com.xbongbong.saas.domain.entity.ext.WorkOrderFormEntityExt;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.WorkOrderEnum;
import com.xbongbong.saas.model.WorkOrderExplainModel;
import com.xbongbong.saas.model.WorkOrderFormModel;
import com.xbongbong.saas.model.WorkOrderTemplateModel;
import com.xbongbong.workorder.explain.WorkOrderExplainHandle;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/01/19 19:05
 */
@Service("workOrderNewPageRenderServiceImpl")
public class WorkOrderNewPageRenderServiceImpl extends AbstractNewPageRenderService<WorkOrderFormEntityExt> {

    @Resource
    private WorkOrderFormModel workOrderFormModel;
    @Resource
    private WorkOrderExplainModel workOrderExplainModel;
    @Resource
    private WorkOrderTemplateModel workOrderTemplateModel;
    @Resource
    private FormDataAddFormExplainHandlerName formDataAddFormExplainHandlerName;
    @Resource
    private WorkOrderExplainHandle workOrderExplainHandle;
    @Resource
    private LowCodeConfigService lowCodeConfigService;
    @Resource
    private CommonHelp commonHelp;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.WORK_ORDER.getCode());
    }

    @Override
    public FormDataAddGetVO addGet(FormDataAddGetDTO formDataAddGetDTO) throws XbbException {
        //从工单全部列表新建，默认使用第一个工单模板
        Long formId = formDataAddGetDTO.getFormId();
        List<WorkOrderFormEntity> workOrderFormList = workOrderFormModel.getTemplateWithPermission(formDataAddGetDTO.getCorpid(), formDataAddGetDTO.getLoginUser(), formDataAddGetDTO.getAppId(), null);
        if (Objects.equals(formId, 0L)) {
            formId = workOrderFormList.get(0).getId();
        }
        formDataAddGetDTO.setFormId(formId);
        FormDataAddGetVO formDataAddGetVO = super.addGet(formDataAddGetDTO);
        workOrderExplainHandle.setTemplate(workOrderFormList, formId, formDataAddGetVO.getExplainList());
        return formDataAddGetVO;
    }

    @Override
    public LowCodeConfigGetVO getLowCodeConfig(LowCodeConfigGetDTO lowCodeConfigGetDTO) throws XbbException {
        // 获取低代码配置
        return lowCodeConfigService.get(lowCodeConfigGetDTO);
    }

    @Override
    public WorkOrderFormEntityExt getForm(FormDataAddGetDTO formDataAddGetDTO) throws XbbException {
        Long formId = formDataAddGetDTO.getFormId();
        String corpid = formDataAddGetDTO.getCorpid();
        return workOrderFormModel.getByKey(formId, corpid);
    }

    @Override
    public List<FieldAttrEntity> getExplain(FormDataAddGetDTO formDataAddGetDTO) throws XbbException {
        Long formId = formDataAddGetDTO.getFormId();
        String corpid = formDataAddGetDTO.getCorpid();
        WorkOrderExplainEntity workOrderExplainEntity = workOrderExplainModel.getByFormId(formId, corpid);
        if (Objects.isNull(workOrderExplainEntity)) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100404);
        }
        Integer businessType = XbbRefTypeEnum.WORK_ORDER.getCode();
        List<FieldAttrEntity> explainList = JSON.parseArray(workOrderExplainEntity.getExplains(), FieldAttrEntity.class);
        //根据套餐判断负责人属性
        Integer feeType = commonHelp.getCurrentCompanyPackage(corpid);
        boolean ultimateTag = Objects.equals(feeType, PackageTypeEnum.ULTIMATE.getType());
        if(!ultimateTag && FormOwnerUniqueEnum.getDefaultBusinessList().contains(businessType)){
            //只有旗舰版支持负责人唯一设置，其他版本拿默认设置
            FormOwnerUniqueEnum formOwnerUniqueEnum = FormOwnerUniqueEnum.getFormOwnerUniqueByBus(businessType);
            Integer multiple = formOwnerUniqueEnum.getValue();
            for (FieldAttrEntity fieldAttrEntity : explainList) {
                if(Objects.equals(fieldAttrEntity.getAttr(), FieldTypeEnum.OWNERID.getAlias())){
                    fieldAttrEntity.setMultiple(multiple);
                }
            }
        }
        return explainList;
    }

    @Override
    public HandlerExplainVO handleExplain(HandlerExplainDTO handlerExplainDTO, WorkOrderFormEntityExt workOrderFormEntityExt) throws XbbException {
        PaasFormEntityExt paasFormEntityExt = new PaasFormEntityExt();
        BeanUtil.copyProperties(workOrderFormEntityExt, paasFormEntityExt);
        paasFormEntityExt.setBusinessType(XbbRefTypeEnum.WORK_ORDER.getCode());
        paasFormEntityExt.setSaasMark(SaasMarkEnum.SAAS.getCode());
        handlerExplainDTO.setPaasFormEntityExt(paasFormEntityExt);
        return formDataAddFormExplainHandlerName.handleExplain(handlerExplainDTO);
    }

    @Override
    public void businessHandleExplain(FormDataAddGetDTO formDataAddGetDTO, List<FieldAttrEntity> explainList, WorkOrderFormEntityExt workOrderFormEntityExt) throws XbbException {

        String corpid = formDataAddGetDTO.getCorpid();
        //工单如果是自由流程，负责人字段显示并必填，指定流程不显示
        WorkOrderTemplateEntity workOrderTemplateEntity = workOrderTemplateModel.getBykey(workOrderFormEntityExt.getTemplateId(), corpid);
        boolean isFree = Objects.equals(workOrderTemplateEntity.getIsFree(), 1);

        for (FieldAttrEntity fieldAttrEntity : explainList) {
            // 获取流水号
            if (!isFree) {
                //指定流程。设置负责人不可见
                if (fieldAttrEntity.getFieldType() != null && fieldAttrEntity.getFieldType().equals(FieldTypeEnum.USER.getType())
                        && Objects.equals(WorkOrderEnum.MANAGER_ID.getAttr(), fieldAttrEntity.getAttr())) {
                    fieldAttrEntity.setVisible(0);
                }
            }
        }

    }

    @Override
    public void formatSaasAttrToReturn(FormDataAddGetDTO formDataAddGetDTO, FormDataAddGetVO formDataAddGetVO, WorkOrderFormEntityExt workOrderFormEntityExt) throws XbbException {
        PaasFormEntityExt paasFormEntityExt = new PaasFormEntityExt();
        BeanUtil.copyProperties(workOrderFormEntityExt, paasFormEntityExt);
        paasFormEntityExt.setBusinessType(XbbRefTypeEnum.WORK_ORDER.getCode());
        paasFormEntityExt.setSaasMark(SaasMarkEnum.SAAS.getCode());
        formDataAddGetVO.setPaasFormEntity(paasFormEntityExt);
    }
}
